package com.learn.learnsatoken.config.token;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Peter Cheung
 * 2023/7/19 13:52
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    @Value("${sa-token.timeout}")
    private long tokenTimeout;
    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {



        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor(handle -> {
            //StpUtil.checkLogin();
            //续约长期有效期，避免redis中大量无效的token
            StpUtil.renewTimeout(tokenTimeout);
        })).addPathPatterns("/api/**");
    }
}
